home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / cfguitrace.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  5.3 KB  |  273 lines

  1. //-----------------------------------------------------------------------------
  2. // File: cfguitrace.cpp
  3. //
  4. // Desc: Contains all trace functionalities used by the UI.
  5. //       Define __CFGUI_TRACE__TO_FILE to have output written to a file.
  6. //       Define __CFGUI_TRACE__TO_DEBUG_OUT to direct output to a debugger.
  7. //       These two symbols can coexist, and are defined in defines.h.
  8. //
  9. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  10. //-----------------------------------------------------------------------------
  11.  
  12. #include "common.hpp"
  13.  
  14.  
  15. #ifndef NTRACE
  16.  
  17.  
  18. const int mindepth = 0;
  19. static int depth = mindepth;
  20.  
  21. static int filedepth = 0;
  22. static FILE *file = NULL;
  23.  
  24. __cfgui_out_filescope::__cfgui_out_filescope(bool bInternal)
  25.     : m_bInternal(bInternal)
  26. {
  27. #ifdef __CFGUI_TRACE__TO_FILE
  28.  
  29.     static bool bFirst = true;
  30.  
  31.     filedepth++;
  32.     
  33.     if (filedepth == 1)
  34.     {
  35.  
  36.         assert(file == NULL);
  37.         if (file == NULL)
  38.             file = fopen("c:\\cfguilog.txt", bFirst ? "w+t" : "a+t");
  39.  
  40.         assert(file != NULL);
  41.         if (file != NULL)
  42.         {
  43.             if (bFirst)
  44.             {
  45.                 time_t curtime;
  46.                 time(&curtime);
  47.                 LPSTR str = _strdup(ctime(&curtime));
  48.                 if (str != NULL)
  49.                 {
  50.                     LPSTR last = str + strlen(str) - 1;
  51.                     if (last >= str && *last == '\n')
  52.                         *last = 0;
  53.                 }
  54.                 fprintf(file,
  55. "\n"
  56. "\n"
  57. "\n"
  58. "--------------------------------------------------------------------------------\n"
  59. "DInput Mapper Device Configuration UI\n"
  60. "New logfile session started at %s.\n"
  61. "--------------------------------------------------------------------------------\n"
  62. "\n"
  63.                         , str);
  64.                 free(str);
  65.  
  66.             }
  67.             bFirst = false;
  68.         }
  69.     }
  70.  
  71. #endif
  72. }
  73.  
  74. __cfgui_out_filescope::~__cfgui_out_filescope()
  75. {
  76. #ifdef __CFGUI_TRACE__TO_FILE
  77.  
  78.     assert(filedepth > 0);
  79.  
  80.     if (filedepth < 0)
  81.         filedepth = 0;
  82.  
  83.     if (filedepth > 0)
  84.     {
  85.         filedepth--;
  86.  
  87.         assert(file != NULL);
  88.         if (file != NULL)
  89.         {
  90.             if (filedepth == 0)
  91.             {
  92.                 fclose(file);
  93.                 file = NULL;
  94.             }
  95.             else if (!m_bInternal)
  96.                 fflush(file);
  97.         }            
  98.     }
  99.  
  100. #endif
  101. }
  102.  
  103. static void __cfgui_out(LPCTSTR str)
  104. {
  105. #ifdef __CFGUI_TRACE__TO_FILE
  106.  
  107.     assert(file != NULL);
  108.     if (file != NULL)
  109.         _ftprintf(file, str);
  110.  
  111. #endif
  112.  
  113. #ifdef __CFGUI_TRACE__TO_DEBUG_OUT
  114.  
  115.     OutputDebugString(str);
  116.  
  117. #endif
  118. }
  119.  
  120. __cfgui_tracescope::__cfgui_tracescope(LPCTSTR str)
  121. {
  122.     if (str != NULL)
  123.         trace(str);
  124.     depth++;
  125. }
  126.  
  127. __cfgui_tracescope::~__cfgui_tracescope()
  128. {
  129.     depth--;
  130. }
  131.  
  132. LPTSTR splitlines(LPTSTR);
  133.  
  134. /*void test(LPTSTR str)
  135. {
  136.     LPTSTR orig = _tcsdup(str), str2 = _tcsdup(str);
  137.     static TCHAR buf[1024];
  138.     int i = 1;
  139.     for (LPTSTR token = splitlines(str2);
  140.          token != NULL;
  141.          token = splitlines(NULL), i++)
  142.     {
  143.         LPTSTR t = _tcsdup(token);
  144.         int len = _tcslen(t);
  145.         BOOL b = t[len - 1] == _T("\n")[0];
  146.         if (b)
  147.             t[len - 1] = _T("\0")[0];
  148.         _stprintf(buf, _T("%02d: \"%s\" (%s)\n"), i, t, BOOLSTR(b));
  149.         __cfgui_out(buf);
  150.         free(t);
  151.     }
  152.     free(str2);
  153.     free(orig);
  154. }
  155. */
  156.  
  157. void __cfgui_trace(__cfgui_tracetype t, LPCTSTR format, ...)
  158. {
  159.     __cfgui_out_filescope fs(true);
  160.     int i;
  161.  
  162.     bool bError = t == __cfgui_tracetype_ERROR;
  163.  
  164.     LPCTSTR errorprefix = _T("ERROR! ");
  165.     const int prefixlen = 8, depthbuflen = 1024, buflen = 4096;
  166.     static TCHAR prefixbuf[prefixlen + depthbuflen + 1] = _T("cfgUI:  "), buf[buflen];
  167.     static LPTSTR depthbuf = prefixbuf + prefixlen;
  168.     static TCHAR space = _T(" ")[0];
  169.     static TCHAR zero = _T("\0")[0];
  170.     static TCHAR endl = _T("\n")[0];
  171.     static int last = -2;
  172.     static bool bendl = true;
  173.  
  174.     if (last == -2)
  175.     {
  176.         for (i = 0; i < depthbuflen; i++)
  177.             depthbuf[i] = space;
  178.         depthbuf[i] = zero;
  179.         last = -1;
  180. /*
  181.         test(_T("aopiwfoiefef\n\nwpoeifef\naefoie\n\n\nwpoeifwef asefeiof"));
  182.         test(_T("\npw\noiefpow ij e f owpiejf\n\n"));
  183.         test(_T("\n\npw\noiefpo wije\n\n   \n\n\nfowpie jf   \n"));
  184. */    }
  185.  
  186.     if (last != -1)
  187.     {
  188.         depthbuf[last] = space;
  189.     }
  190.  
  191.     int d = depth;
  192.     if (d < mindepth)
  193.         d = mindepth;
  194.     
  195.     last = d * 4;
  196.     if (last >= depthbuflen)
  197.         last = depthbuflen - 1;
  198.  
  199.     depthbuf[last] = zero;
  200.  
  201.     va_list args;
  202.     va_start(args, format);
  203. #ifdef WIN95
  204.     {
  205.         char *psz = NULL;
  206.         char szDfs[1024]={0};
  207.         strcpy(szDfs,format);                    // make a local copy of format string
  208.         while (psz = strstr(szDfs,"%p"))        // find each %p
  209.             *(psz+1) = 'x';                        // replace each %p with %x
  210.         _vstprintf(buf, szDfs, args);               // use the local format string
  211.     }
  212. #else
  213.     {
  214.         _vstprintf(buf, format, args);
  215.     }
  216. #endif
  217.     va_end(args);
  218.  
  219.     LPTSTR tempbuf = _tcsdup(buf);
  220.  
  221.     bool doprefix = bendl;
  222.     for (LPTSTR token = splitlines(tempbuf);
  223.          token != NULL;
  224.          token = splitlines(NULL))
  225.     {
  226.         if (doprefix)
  227.             __cfgui_out(depthbuf/*prefixbuf*/);
  228.         if (bError && doprefix)
  229.             __cfgui_out(errorprefix);
  230.         __cfgui_out(token);
  231.         bendl = token[_tcslen(token) - 1] == endl;
  232.         doprefix = bendl;
  233.     }
  234.  
  235.     free(tempbuf);
  236. }
  237.  
  238. LPTSTR splitlines(LPTSTR split)
  239. {
  240.     static LPTSTR str = NULL;
  241.     static int last = 0;
  242.     static TCHAR save = _T("!")[0];
  243.     static TCHAR newline = _T("\n")[0], zero = _T("\0")[0];
  244.  
  245.     if (split != NULL)
  246.         str = split;
  247.     else
  248.     {
  249.         if (str == NULL)
  250.             return NULL;
  251.  
  252.         str[last] = save;
  253.         str += last;
  254.     }
  255.  
  256.     if (str[0] == zero)
  257.     {
  258.         str = NULL;
  259.         return NULL;
  260.     }
  261.  
  262.     LPCTSTR at = str, f = _tcschr(at, newline);
  263.     last = f ? f - at + 1: _tcslen(at);
  264.  
  265.     save = str[last];
  266.     str[last] = zero;
  267.  
  268.     return str;
  269. }
  270.  
  271.  
  272. #endif
  273.